<?php

namespace Biz\model\advertisement;


class MPositionProjectRelations extends MBaseDbModel
{
    const _CONFIG_FILE_ = 'db_position';

    const _CONFIG_SELECT_ = 'db_position';

    const _DATABASE_ = 'hhz_position';

    const _TABLE_NAME = 'position_project_relations';

    const PROJECT_STATUS_OK = 1;
    const PROJECT_STATUS_RELEASE = 2;

    const SHARE_OK = 1;
    const SHARE_NO = 2;


    const EXCLUSIVE = 1; // 独占
    const NOT_EXCLUSIVE = 0; // 非独占

    public static function getMonthlyList($month)
    {
        //当月第一天
        $startTime = date('Y-m-01 00:00:00', strtotime($month));
        //当月最后一天
        $endTime = date('Y-m-d 23:59:59', strtotime($startTime . " +1 month -1 day"));
        return self::select(
            [
                "[>]" . MPositionProject::_TABLE_NAME => ["project_id" => "id"],
                "[>]" . MHhzAdmin::_TABLE_NAME => [MPositionProject::_TABLE_NAME . ".admin_id" => "id"],
                "[>]" . MHhzDepartment::_TABLE_NAME => [MPositionProject::_TABLE_NAME . ".department_id" => "id"],
            ],
            [
                self::_TABLE_NAME . ".id",
                self::_TABLE_NAME . ".ad_id",
                self::_TABLE_NAME . ".department_id",
                self::_TABLE_NAME . ".project_id",
                self::_TABLE_NAME . ".start_show_time",
                self::_TABLE_NAME . ".end_show_time",
                self::_TABLE_NAME . ".is_share",
                self::_TABLE_NAME . ".is_exclusive",
                self::_TABLE_NAME . ".ad_unique",
                MPositionProject::_TABLE_NAME . ".project_name",
                MPositionProject::_TABLE_NAME . ".project_desc",
                MPositionProject::_TABLE_NAME . ".admin_id",
                MPositionProject::_TABLE_NAME . ".is_sold",
                MHhzAdmin::_TABLE_NAME . ".account",
                MHhzAdmin::_TABLE_NAME . ".username",
                MHhzDepartment::_TABLE_NAME . ".name",
            ],
            [
                self::_TABLE_NAME . ".project_status" => self::PROJECT_STATUS_OK,
                MPositionProject::_TABLE_NAME . ".project_status" => MPositionProject::PROJECT_STATUS_OK,
                self::_TABLE_NAME . ".start_show_time[<=]" => $endTime,
                self::_TABLE_NAME . ".end_show_time[>=]" => $startTime,
            ]
        );
    }

    public static function getDailyList($day)
    {
        $day = date('Y-m-d', strtotime($day));
        return self::select(
            [
                "[>]" . MPositionProject::_TABLE_NAME => ["project_id" => "id"],
                "[>]" . MHhzAdmin::_TABLE_NAME => [MPositionProject::_TABLE_NAME . ".admin_id" => "id"],
                "[>]" . MHhzDepartment::_TABLE_NAME => [MPositionProject::_TABLE_NAME . ".department_id" => "id"],
            ],
            [
                self::_TABLE_NAME . ".id",
                self::_TABLE_NAME . ".ad_id",
                self::_TABLE_NAME . ".department_id",
                self::_TABLE_NAME . ".project_id",
                self::_TABLE_NAME . ".start_show_time",
                self::_TABLE_NAME . ".end_show_time",
                self::_TABLE_NAME . ".is_exclusive",
                MPositionProject::_TABLE_NAME . ".project_name",
                MPositionProject::_TABLE_NAME . ".project_desc",
                MPositionProject::_TABLE_NAME . ".admin_id",
                MPositionProject::_TABLE_NAME . ".is_sold",
                MHhzAdmin::_TABLE_NAME . ".account",
                MHhzAdmin::_TABLE_NAME . ".username",
                MHhzDepartment::_TABLE_NAME . ".name",
            ],
            [
                self::_TABLE_NAME . ".project_status" => self::PROJECT_STATUS_OK,
                MPositionProject::_TABLE_NAME . ".project_status" => MPositionProject::PROJECT_STATUS_OK,
                self::_TABLE_NAME . ".start_show_time[<=]" => $day . " 23:59:59",
                self::_TABLE_NAME . ".end_show_time[>=]" => $day . " 00:00:00",
            ]
        );
    }


    public static function getExclusiveList()
    {
        $day = date('Y-m-d', time());
        return self::select(
            [
                "[>]" . MPositionProject::_TABLE_NAME => ["project_id" => "id"],
                "[>]" . MHhzAdmin::_TABLE_NAME => [MPositionProject::_TABLE_NAME . ".admin_id" => "id"],
                "[>]" . MHhzDepartment::_TABLE_NAME => [MPositionProject::_TABLE_NAME . ".department_id" => "id"],
            ],
            [
                self::_TABLE_NAME . ".id",
                self::_TABLE_NAME . ".ad_id",
                self::_TABLE_NAME . ".department_id",
                self::_TABLE_NAME . ".project_id",
                self::_TABLE_NAME . ".start_show_time",
                self::_TABLE_NAME . ".end_show_time",
                self::_TABLE_NAME . ".is_exclusive",
                MPositionProject::_TABLE_NAME . ".project_name",
                MPositionProject::_TABLE_NAME . ".project_desc",
                MPositionProject::_TABLE_NAME . ".admin_id",
                MPositionProject::_TABLE_NAME . ".is_sold",

            ],
            [
                self::_TABLE_NAME . ".project_status" => self::PROJECT_STATUS_OK,
                MPositionProject::_TABLE_NAME . ".project_status" => MPositionProject::PROJECT_STATUS_OK,
                self::_TABLE_NAME . ".end_show_time[>=]" => $day . " 00:00:00",
                self::_TABLE_NAME. ".is_exclusive" => self::EXCLUSIVE
            ]
        );
    }

    public static function getBgColorById($id)
    {
        $colors = [
            '#DB595E',
            '#3182C4',
            '#8AC926',
            '#E0B326',
            '#52A675',
            '#C5CA2F',
            '#4266AC',
            '#51B9B5',
            '#DB8042',
            '#6A4C93',
            '#188918',
            '#BD904E',
            '#A34584',
            '#4E9ACC',
            '#DE6C40',
        ];

        return $colors[bcmod($id, count($colors))];
    }

    public static function getById($id)
    {
        return self::get("*", ['id' => $id]);
    }
}